package cloudContact.DAO;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Hashtable;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

/// Sample code to use: 
/// 	Gson gson = new GsonBuilder().registerTypeAdapter(Id.class, new IdSerializer()).create();
///
public class GoogleContactSerializer implements JsonSerializer<GoogleContact>{

	@Override
	public JsonElement serialize(GoogleContact target, Type typeOfGoogleContact,
			JsonSerializationContext context) {
		JsonObject object = new JsonObject(); 
		
		object.add("full_name", context.serialize(target.getFullName()));
//		object.add("first_name", context.serialize(target.getFirstName()));
//		object.add("last_name", context.serialize(target.getLastName()));
//		object.add("middle_name", context.serialize(target.getMiddleName()));
//		object.add("work_phone", context.serialize(target.getWorkPhone()));
//		object.add("home_phone", context.serialize(target.getHomePhone()));
		object.add("mobile_phone", context.serialize(target.getMobilePhone()));
		object.add("facebook", context.serialize(target.getFacebookURL()));
		object.add("address", context.serialize(target.getAddress()));
		object.add("email", context.serialize(target.getEmail()));
		object.add("notes", context.serialize(target.getNotes()));
		
//		object.add("birthday", context.serialize(target.getBirthday()));
//		object.add("title", context.serialize(target.getTitle()));
//		object.add("last_seen_name", context.serialize(target.getLastSeenName()));
		object.add("lati", context.serialize(target.getLastSeenLati()));
		object.add("longi", context.serialize(target.getLastSeenLongi()));
		object.add("id", context.serialize(target.getContactId()));
		
//		Hashtable<String, String> allExtensions = target.getAllExtensions();
//		JsonArray extensionArray = new JsonArray();
//		
//		while (allExtensions.keys().hasMoreElements()){
//			String key = allExtensions.keys().nextElement();
//			String value = allExtensions.get(key);
//			JsonObject extensionObject = new JsonObject();
//			extensionObject.add("key", context.serialize(key));
//			extensionObject.add("value", context.serialize(value));
//			extensionArray.add(extensionObject);
//		}
//		object.add("extensions", extensionArray);
		
		ArrayList<String> groups = target.getGroups();
		JsonArray groupArray = new JsonArray();
		
		for(String group : groups){
			JsonObject groupObject = new JsonObject();
			groupObject.add("name", context.serialize(group));
			groupArray.add(groupObject);
		}
		object.add("groups", groupArray);
		
		return object;
	}

}
